import urllib.request
import io
from PIL import Image
from bs4 import BeautifulSoup
import pyperclip
import re
from urllib.parse import unquote

hostname = "http://www.zhainandy3.xyz"
def getLinks():
    
    f = urllib.request.urlopen(f'{hostname}/vod/24-1.html')
    soup = BeautifulSoup(f.read().decode('utf-8'),features="html.parser")
    return [(hostname + link.img["data-original"],link.img["alt"],hostname + link["href"]) for link in soup.find_all("a", class_="playpic")]
    
    
import tkinter as tk

class Application(tk.Frame):
    def __init__(self, master=None,url = ""):
        super().__init__(master)
        self._url = url
        self.master = master
        self.pack()
        self.create_widgets()

    def create_widgets(self):
        image_bytes = urllib.request.urlopen(self._url[0]).read()
        data_stream = io.BytesIO(image_bytes)
        pil_image = Image.open(data_stream)
        pil_image.save("tkwindow.png")
        self.img = tk.PhotoImage(file = "tkwindow.png")
        self.hi_there = tk.Button(self)
        self.hi_there["command"] = self.say_hi
        self.hi_there["image"] = self.img
        self.hi_there.pack(side="top")
        
        self.hi_there1 = tk.Label(self)
        self.hi_there1["text"] = self._url[1]
        self.hi_there1.pack(side="top")

        self.quit = tk.Button(self, text="QUIT", fg="red", command=self.master.destroy)
        self.quit.pack(side="bottom")

    def say_hi(self):
        f = urllib.request.urlopen(self._url[2])
        soup = BeautifulSoup(f.read().decode('utf-8'),features="html.parser")
        for link in soup.findAll("div", class_="playlist"):
            for alink in link.find_all("a"):
                jsContentLines = urllib.request.urlopen(hostname + alink["href"].replace("html", "js")).read().decode('utf-8').split(';')
                jsContent = jsContentLines[len(jsContentLines)-2]
                pattern = re.compile(r'\"([^\"]+)".*')
                for y in re.findall(pattern, jsContent):
                    llls = unquote(y).split("++")
                    print(llls[1])
                    pyperclip.copy(llls[1])

for x in getLinks():
    root = tk.Tk()
    app = Application(master=root,url=x)
    app.mainloop()
    